package com.gfxy.modules.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gfxy.modules.base.vo.TeacherBaseInfoVo;
import com.gfxy.modules.common.entity.TreeItemModel;
import com.gfxy.modules.common.mapper.GfxyCommonMapper;
import com.gfxy.modules.common.service.IGfxyCommonService;
import com.gfxy.modules.system.entity.SysDepart;
import com.gfxy.modules.system.entity.SysUser;
import com.gfxy.modules.system.mapper.SysDepartMapper;
import com.gfxy.modules.system.model.SysDepartTreeModel;
import com.gfxy.modules.system.util.FindsDepartsChildrenUtil;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.vo.DictModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.annotations.Cacheable;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lijixin 2021/11/12 11:23 上午
 */
@Service
public class GfxyCommonService implements IGfxyCommonService {

    @Autowired
    private GfxyCommonMapper gfxyCommonMapper;

    @Autowired
    private SysDepartMapper sysDepartMapper;

    @Override
    public List<TeacherBaseInfoVo> getTeachingTeacherByOrgCode(String orgCode){
        List<TeacherBaseInfoVo> sysUsers = gfxyCommonMapper.getTeacherByOrgCodeAndTeacherType(orgCode,"1-1");
        return sysUsers;
    }

    public List<TreeItemModel> getTreeModelForOrg(){
        LambdaQueryWrapper<SysDepart> query = new LambdaQueryWrapper<SysDepart>();
        query.and(wp -> wp.eq(SysDepart::getOrgType,"3").or().eq(SysDepart::getOrgType,"0"));
        query.eq(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        query.orderByAsc(SysDepart::getDepartOrder);
        List<SysDepart> list = sysDepartMapper.selectList(query);

        TreeItemModel parentNode = null;
        List<TreeItemModel> treeItemModels = new ArrayList<>();
        for (SysDepart sysDepart : list) {
            TreeItemModel pNode = new TreeItemModel();
            pNode.setId(sysDepart.getId());
            pNode.setKey(sysDepart.getId());
            pNode.setValue(sysDepart.getDepartName());
            pNode.setTitle(sysDepart.getDepartName());
            pNode.setLeaf(false);
            if(sysDepart.getParentId().equals("3")){

                pNode.setParentId("A05");
                treeItemModels.add(pNode);
            } else {
                pNode.setParentId("");
                parentNode = pNode;
            }
        }


        if(parentNode == null){
            return treeItemModels;
        } else {
            List<TreeItemModel> result = new ArrayList<>();
            parentNode.setChildren(treeItemModels);
            result.add(parentNode);
            System.out.println(result);
            return result;
        }
    }


    @Override
    public List<TreeItemModel> getTreeModelTeachingTeacherByOrgCode(String orgCode) {
//        gfxyCommonMapper.getTeachingTeacherByOrgCode(orgCode);
        List<TreeItemModel> child = new ArrayList<>();
        List<TeacherBaseInfoVo> sysUsers = gfxyCommonMapper.getTeacherByOrgCodeAndTeacherType(orgCode,"1-1");
        for (TeacherBaseInfoVo sysUser : sysUsers) {
            TreeItemModel cNode = new TreeItemModel();
            cNode.setId(sysUser.getId());
            cNode.setKey(sysUser.getId());
            cNode.setParentId(orgCode);
            cNode.setValue(sysUser.getRealname());
            cNode.setTitle(sysUser.getRealname());
            cNode.setLeaf(true);
            child.add(cNode);
        }
        return child;
    }

    @Override
    public List<TreeItemModel> getTreeModelTeacherByOrgCode(String orgCode) {
        List<TreeItemModel> child = new ArrayList<>();
        List<TeacherBaseInfoVo> sysUsers = gfxyCommonMapper.getTeacherByOrgCode(orgCode);
        for (TeacherBaseInfoVo sysUser : sysUsers) {
            TreeItemModel cNode = new TreeItemModel();
            cNode.setId(sysUser.getId());
            cNode.setKey(sysUser.getId());
            cNode.setParentId(orgCode);
            cNode.setValue(sysUser.getRealname());
            cNode.setTitle(sysUser.getRealname());
            cNode.setLeaf(true);
            child.add(cNode);
        }
        return child;
    }

    @Override
    @Cacheable(value = "gfxy-teaching-department")
    public List<DictModel> queryTeachingDepartment() {
        List<DictModel> result = new ArrayList<>();
        List<SysDepart> sysDeparts = sysDepartMapper.queryDeptByPid("3");
        for (SysDepart sysDepart : sysDeparts) {
            DictModel model = new DictModel();
            model.setValue(sysDepart.getId());
            model.setText(sysDepart.getDepartName());
            result.add(model);
        }
        DictModel model = new DictModel();
        model.setValue("A05");
        model.setText("其他");
        result.add(model);
        return result;
    }


    @Override
    public List<TreeItemModel> getTreeModelByOrgCode(String orgCode) {

        List<TreeItemModel> result = new ArrayList<>();

        LambdaQueryWrapper<SysDepart> query = new LambdaQueryWrapper<SysDepart>();
        query.eq(SysDepart::getOrgCode, orgCode);
        query.and(wp -> wp.eq(SysDepart::getOrgType,"3").or().eq(SysDepart::getOrgType,"0"));
        query.eq(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        query.orderByAsc(SysDepart::getDepartOrder);
        List<SysDepart> list = sysDepartMapper.selectList(query);
        SysDepart depart = list.get(0);
        TreeItemModel pNode = new TreeItemModel();
        pNode.setId(depart.getId());
        pNode.setKey(depart.getId());

        pNode.setParentId("");
        pNode.setValue(depart.getDepartName());
        pNode.setTitle(depart.getDepartName());
        pNode.setLeaf(false);
        pNode.setChildren(this.getTreeModelTeachingTeacherByOrgCode(orgCode));

        result.add(pNode);


        return result;
    }
}
